package com.hutu.controller.biz.admin;

import com.hutu.model.resp.Response;
import com.hutu.model.resp.ResponseVo;
import com.hutu.service.biz.SqlService;
import com.hutu.service.biz.TableInfoService;
import com.hutu.model.biz.TableInfo;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.hutu.util.HtmlEscaper;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  Controller
 * </p>
 *
 * @author admin
 * @since 2023-07-14
 */
@RestController
@RequestMapping("/admin/table-info")
@Api(tags = "表信息")
public class TableInfoController {
    @Resource
    private TableInfoService tableInfoService;
    @Resource
    private SqlService sqlService;



    @ApiOperation("新增")
    @PostMapping("add")
    public ResponseVo add(@RequestBody TableInfo tableInfo){
            return this.tableInfoService.add(tableInfo);
    }

    @ApiOperation("新增空行")
    @GetMapping("addBlankRow")
    public ResponseVo addBlankRow(Integer tid){
        this.tableInfoService.addBlankRow(tid);
        return Response.success();
    }

    @ApiOperation("从SQL新增")
    @PostMapping("addFromSql")
    public ResponseVo addFromSql(@RequestBody TableInfo tableInfo){
        com.hutu.model.entity.core.TableInfo tableSchema = sqlService.parseSql(tableInfo.getSql());
        tableSchema.setPid(tableInfo.getPid()+"");
        return this.tableInfoService.addTableSchema(tableSchema);
    }

    @ApiOperation("逻辑删除")
    @DeleteMapping("/{id}")
    public ResponseVo delete(@PathVariable Integer id){
            return this.tableInfoService.deleteById(id);
    }

    @ApiOperation("批量逻辑删除")
    @DeleteMapping("/batch/{ids}")
    public ResponseVo deleteBatch(@PathVariable List<Integer> ids){
            return this.tableInfoService.deleteBatch(ids);
    }

    @ApiOperation("编辑更新")
    @PutMapping
    public ResponseVo edit(@RequestBody TableInfo tableInfo){
            return this.tableInfoService.edit(tableInfo);
    }

    @ApiOperation("查询")
    @GetMapping("/{id}")
    public ResponseVo findById(@PathVariable Integer id){
            return this.tableInfoService.findById(id);
    }

    @ApiOperation("分页条件查询")
    @GetMapping("/page")
    public ResponseVo list(@RequestParam(defaultValue = "1") Integer current,@RequestParam(defaultValue = "10") Integer size
            ,Integer projectId,String tableName){
            return this.tableInfoService.list(current,size,projectId,tableName);
    }

    @ApiOperation("数据导出接口")
    @GetMapping("/export")
    public ResponseVo export(HttpServletResponse response)throws IOException {
            return this.tableInfoService.export(response);
    }

    @ApiOperation("数据导入接口")
    @PostMapping("/import")
    public ResponseVo imp(MultipartFile file)throws IOException{
            return this.tableInfoService.imp(file);
    }


    @ApiOperation("生成Entity")
    @GetMapping("createEntity/{id}/{project}")
    public ResponseVo createEntity(@PathVariable Integer id,@PathVariable String project){
        return this.tableInfoService.createEntity(id,project);
    }

    @ApiOperation("生成Vo")
    @GetMapping("createVo/{id}/{project}")
    public ResponseVo createVo(@PathVariable Integer id,@PathVariable String project){
        return this.tableInfoService.createVo(id,project);
    }

    @ApiOperation("生成Mapper.java")
    @GetMapping("createMapper/{id}/{project}")
    public ResponseVo createMapperJ(@PathVariable Integer id,@PathVariable String project){
        return this.tableInfoService.createMapper(id,project);
    }

    @ApiOperation("生成Mapper.xml")
    @GetMapping("createMapper2/{id}/{project}")
    public ResponseVo createMapper2(@PathVariable Integer id,@PathVariable String project){
        ResponseVo mapper2 = this.tableInfoService.createMapper2(id, project);
        ResponseVo vo = (ResponseVo) mapper2.getData();
        String escapeHtml = HtmlEscaper.escapeHtml(vo.getData().toString());
        vo.setData(escapeHtml);
        return mapper2;
    }
}

